package com.nlp.controller;

import com.alibaba.fastjson.JSONObject;
import com.nlp.solve.Main;
import com.nlp.solve.issue_a.Issue_A;
import com.nlp.solve.issue_b.Issue_B;
import com.nlp.solve.issue_d.Issue_D;
import com.nlp.solve.issue_g.Issue_G;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class IndexController {
    @Autowired
    private Issue_A issue_a;
    @Value("${input.file.dir}")
    private String inputDir;
    @Value("${output.file.dir}")
    private String outputDir;

    //将 result.json转换为issue_a.json
    @GetMapping(value = "/issue_a", produces = {"application/json;charset=utf-8"})
    public String issue_A() throws NoSuchMethodException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        File file = new File(outputDir + "issue_a\\");
        String abs = outputDir + "result.json";
        if(!file.exists()|| Objects.requireNonNull(file.listFiles()).length==0){
            //生成country_statistic.json
            issue_a.solve(inputDir, outputDir);
            //生成result.json
            issue_a.issue_a(abs, outputDir);
            //转换result.json -> issue_a_all.json
        }
        abs = issue_a.transfer(abs, outputDir + "issue_a\\");
        return Main.read(abs);
    }

    @GetMapping(value = "/issue_a/{country}", produces = {"application/json;charset=utf-8"})
    public String issue(@PathVariable("country") String country) throws NoSuchMethodException, IOException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        String abs = outputDir + "/issue_a/" + country + ".json";
        File file = new File(abs);
        if (!file.exists()) {
            issue_A();
        }
        return Main.read(abs);
    }

    @Autowired
    private Issue_B issue_b;

    @GetMapping(value = "/issue_b", produces = {"application/json;charset=utf-8"})
    public String issue_b() throws IOException {
        String abs = outputDir + "result.json";
        //生成result.json
        String s = issue_b.Issue_B(abs, outputDir + "issue_b\\");
        return Main.read(s);
    }

    @GetMapping(value = "/issue_c", produces = {"application/json;charset=utf-8"})
    public String issue_c() throws IOException {
        String input = outputDir+"issue_b\\issue_b.json";
        Map<String, Map<String, Integer>> map = JSONObject.parseObject(Main.read(input), Map.class);
        //国家 : 次数
        Map<String, Integer> res = new HashMap<>();
        for (Map.Entry<String, Map<String, Integer>> entry : map.entrySet()) {
            for (Map.Entry<String, Integer> e : entry.getValue().entrySet()) {
                res.put(entry.getKey(), res.getOrDefault(entry.getKey(), 0) + e.getValue());
            }
        }
        return JSONObject.toJSONString(res);
    }

    @Autowired
    private Issue_D issue_d;

    @GetMapping(value = "/issue_d", produces = {"application/json;charset=utf-8"})
    public String issue_d() throws IOException {
        String output = outputDir+"issue_d\\issue_d.json";
        File file = new File(output);
        if (!file.exists()) {
            issue_d.resolve(outputDir, output);
        }
        return Main.read(output);
    }

    @Autowired
    private Issue_G issue_g;

    @GetMapping(value = "/issue_g", produces = {"application/json;charset=utf-8"})
    public String issue_g() throws IOException {
        String outputPath = outputDir+"issue_g\\issue_g.json";
        File file = new File(outputPath);
        if(!file.exists()){
             issue_g.solve(outputDir, outputPath);
        }
        return Main.read(outputPath);
    }
}
